<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- <div>员工</div> -->
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button size="mini" icon="el-icon-plus" type="primary" @click="showDialog=true">新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table v-loading="loading" border="" :data="list">
              <el-table-column type="index" width="80" align="center" header-align="center" label="序号" />
              <el-table-column header-align="center" align="center" prop="name" label="角色" width="240" />
              <el-table-column header-align="center" align="center" prop="description" label="描述" />
              <el-table-column header-align="center" align="center" label="操作">
                <!-- 作用域插槽 -->
                <template slot-scope="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页 -->
              <el-pagination layout="prev, pager, next" :total="page.total" :page-size="page.pagesize" @current-change="changePage" />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <!-- 公司信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="企业名称:">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址:">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司电话:">
                <el-input v-model="formData.companyPhone" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱:">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注:">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 弹层 -->
    <el-dialog :title="showTitle" :visible="showDialog" @close="btnCancel">
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole } from '@/api/setting'
import { mapGetters } from 'vuex'
export default {
  name: 'EmPloyees',
  data() {
    return {
      loading: false,
      list: [], // 所有的角色
      page: {
        paeg: 1,
        pagesize: 10,
        total: 0
      },
      // 专门接收新增或者编辑的编辑的表单数据
      roleForm: {},
      formData: {}, // 公司信息
      showDialog: false,
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    showTitle() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }
  },
  created() {
    this.getRoleList() // 获取角色列表
    this.getCompanyInfo() // 获取公司信息
  },
  methods: {
    async getRoleList() {
      this.loading = true // 打开状态
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
      this.loading = false
    },
    // 分页
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      this.getRoleList()
    },
    // 这是公司的基本信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId) // 赋值给formData
    },
    // 删除
    deleteRole(id) {
      this.$confirm('您是否确定要删除该角色吗').then(() => {
        return deleteRole(id)
      }).then(() => {
        this.$message.success('删除成功')
        this.getRoleList()
      })
    },
    // 编辑
    async editRole(id) {
      this.roleForm = await getRoleDetail(id) // 实现数据的回写
      this.showDialog = true // 点击弹层 // 为了不出现闪烁的问题 先获取数据 再弹出层
    },
    // 确定
    async btnOK() {
      try {
        this.$refs.roleForm.validate()
        // 只有校验通过的情况下才会执行await的下方内容
        if (this.roleForm.id) {
          // 编辑
          await updateRole(this.roleForm)
        } else {
          // 新增
          await addRole(this.roleForm)
        }
        this.getRoleList()
        this.showDialog = false // 关闭弹层 => 会触发close事件
      } catch (error) {
        console.log(error)
      }
    },
    // 取消
    btnCancel() {
      this.roleForm = {}
      // 移除校验
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    }
  }
}
</script>

<style scoped>
/deep/.el-form-item__content{
  margin-right: 40px;
}
</style>
